package com.mooc.meetingfilm.user.controller;

import com.mooc.meetingfilm.utils.common.vo.LoginParamVO;
import com.mooc.meetingfilm.user.dao.entity.MoocUserT;
import com.mooc.meetingfilm.user.service.UserService;
import com.mooc.meetingfilm.utils.util.JwtTokenUtil;
import com.mooc.meetingfilm.utils.common.vo.BaseResponseVO;
import com.mooc.meetingfilm.utils.exception.BaseServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public BaseResponseVO login(@RequestBody LoginParamVO loginParamVO) throws BaseServiceException {
        loginParamVO.check();

        MoocUserT user = userService.checkLoginParamVo(loginParamVO);

        JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
        String randomKey = jwtTokenUtil.getRandomKey();
        String token = jwtTokenUtil.generateToken(user.getUserName(), randomKey);


        Map<String,String> map = new HashMap<>();
        map.put("randomKey", randomKey);
        map.put("token", token);
        return BaseResponseVO.success(map);
    }
}
